<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use SimpleXMLElement;

class Common extends Controller
{
    
	public  function __construct(Request $request){
        if($request->isMobile()){
           config('template.view_path', VIEW_PATH.'mobile/'); //设置手机模板
        }
        parent::__construct($request);
    }
	
	public function _initialize(){		
		if (!is_file(APP_PATH . 'install/data/install.lock')) {
			header("Location: index.php/install/index");
			exit;
		}
		//站点状态
		$isopen = db('site')->field('webstatus,webclosedesc')->where('id=1')->find();	
		if($isopen['webstatus']==0){
			exit($isopen['webclosedesc']);
		}
		spider(); //蜘蛛爬虫		
		$this->assign("nav",$this->nav());
		$this->assign("catelist",$this->catelist());			
    }	
	
	//导航
	public function nav(){
		$list = db('nav')->where('isshow=1')->field('showcate,id,title,entitle')->order('sort,id')->select();
		if($list){
			foreach($list as $key=>$v){				
				$list[$key]['link'] = '/'.$v['entitle'].'/';				
				if($v['showcate']==1){
					$list[$key]['son'] = db('cate')->where("nid=".$v['id']." and isshow=1 and pid=0")->field('id,title,entitle')->order("sort,id")->select();
					if($list[$key]['son'])foreach($list[$key]['son'] as $key1=>$v1){						
						$list[$key]['son'][$key1]['link'] = '/'.$v1['entitle'].'/';
					}
				}else{
					$list[$key]['son'] = db('article')->where("nid=".$v['id'])->field('title,id')->order("sort,id")->select();
					if($list[$key]['son'])foreach($list[$key]['son'] as $key2=>$v2){
						$list[$key]['son'][$key2]['link'] = '/'.$v['entitle'].'/'.$v2['id'].'/';
					}
				}
			}
		}
		return $list;		
	}
	
	//案例展厅
	public function catelist(){
		$list = db('cate')->field('img,entitle,title,id')->where('nid=3')->order('sort,id')->limit(8)->select();
		if($list)foreach($list as $key=>$v){
			$list[$key]['son'] = db('article')->field('id,title,note,img')->where('nid=3 and cid='.$v['id'])->order('istop desc,sort desc,id desc')->limit(3)->select();
		}
		return $list;
	}
	
}
